﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guline.Web.LiveModels.Models;
using Guline.Web.LiveModels.DataContext;

namespace Guline.Web.Livechat.Controllers
{
    public class Room_memberController : Controller
    {
        private DataContext db = new DataContext();

        //abc
        // GET: /Room_member/
       public ActionResult jsonList()
        {


            var list = (from n in db.Room_members select new {n.Room.room_name,n.User.username,n.isroot,n.add_date,n.ID });
			
            return Json(list, JsonRequestBehavior.AllowGet);
        } 
		
        public ActionResult Index()
        {

            return View();

        }

        //
        // GET: /Room_member/Details/5

        public ActionResult Details(int id = 0)
        {
            Room_member room_member = db.Room_members.Find(id);
            if (room_member == null)
            {
                return HttpNotFound();
            }
            return View(room_member);
        }

        //
        // GET: /Room_member/Create

        public ActionResult Create()
        {
            ViewBag.room_ID = new SelectList(db.Rooms, "ID", "room_name");
            ViewBag.user_ID = new SelectList(db.Users, "ID", "username");
            return View();
        }

        //
        // POST: /Room_member/Create
		   [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(Room_member room_member)
        {
            if (ModelState.IsValid)
            {
                room_member.add_date = DateTime.Now;
                db.Room_members.Add(room_member);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.room_ID = new SelectList(db.Rooms, "ID", "room_name", room_member.room_ID);
            ViewBag.user_ID = new SelectList(db.Users, "ID", "username", room_member.user_ID);
            return View(room_member);
        }

        //
        // GET: /Room_member/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Room_member room_member = db.Room_members.Find(id);
            if (room_member == null)
            {
                return HttpNotFound();
            }
            ViewBag.room_ID = new SelectList(db.Rooms, "ID", "room_name", room_member.room_ID);
            ViewBag.user_ID = new SelectList(db.Users, "ID", "username", room_member.user_ID);
            return View(room_member);
        }

        //
        // POST: /Room_member/Edit/5
		   [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(Room_member room_member)
        {
            if (ModelState.IsValid)
            {
                room_member.add_date = DateTime.Now;
                db.Entry(room_member).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.room_ID = new SelectList(db.Rooms, "ID", "room_name", room_member.room_ID);
            ViewBag.user_ID = new SelectList(db.Users, "ID", "username", room_member.user_ID);
            return View(room_member);
        }

        //
        // GET: /Room_member/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Room_member room_member = db.Room_members.Find(id);
            if (room_member == null)
            {
                return HttpNotFound();
            }
            return View(room_member);
        }

        //
        // POST: /Room_member/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Room_member room_member = db.Room_members.Find(id);
            db.Room_members.Remove(room_member);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
		[HttpPost]
        public ActionResult ajDelete(int id)
        {
             Room_member room_member = db.Room_members.Find(id);
			 if (room_member == null)
            {
                return Json(new { success = false ,msg="Không tìm thấy đối tượng"});
            }

         
            db.Room_members.Remove(room_member);
			db.SaveChanges();
            return Json(new { success=true});
        }
		
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}